package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.TbAddress;
import com.dongdongshop.pojo.TbProvinces;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.AddRessService;
import com.dongdongshop.service.ProvincesService;
import com.dongdongshop.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @DubboReference
    private AddRessService addRessService;

    @DubboReference
    private ProvincesService provincesService;

    @RequestMapping("index")
    public String index(){

        return "index";
    }

    @RequestMapping("register")
    public String register(){
        return "register";
    }

    @RequestMapping("obtainCode")
    @ResponseBody
    public Result obtainCode(String phone){
        try{
            rocketMQTemplate.convertAndSend("my_code_topic",phone);
            return Result.ok().setData("发送成功");
        }catch (Exception e){
            return Result.respones(ResultEnum.ERROR).setData("发送失败");
        }
    }

    @RequestMapping("getOrderInfo")
    public String getOrderInnfo(Model model){
        TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
        List<TbAddress> addressList =addRessService.getAddressInfo(user.getUsername());
        model.addAttribute("address",addressList);
        return "getOrderInfo";
    }

    @RequestMapping("pay")
    public String pay(){
        return "pay";
    }

    @RequestMapping("home")
    public String home(){

        return "home-index";
    }

    @RequestMapping("homeSettingAddRess")
    public String homeSettingAddRess(Model model){
        List<TbProvinces> provinces = provincesService.getHomeSettinfAddressInfo();
        model.addAttribute("pro",provinces);
        return "home-setting-address";
    }

    @RequestMapping("homeOrderSend")
    public String homeOrderSend(){

        return "home-order-send";
    }

    @RequestMapping("refundsuccess")
    public String refundsuccess(){

        return "refundsuccess";
    }



}
